/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package replica.transaction;

import generalResourceLalla.TaskList;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import replica.stableMemory.StableFile;

/**
 *
 * @author andrea
 */
public abstract class LogRegister 
{
    List<TransactionStatus> list=new LinkedList<TransactionStatus>();
    long idFrontEnd;
    StableFile<TransactionStatus> stableFile;
    
    public LogRegister(long idFrontEnd, StableFile<TransactionStatus> stableFile)
    {
        this.idFrontEnd=idFrontEnd;
        this.stableFile=stableFile;
        //File file = new File(getNameFile());
    }
    
    public void setStableFile(String name)
    {
        this.stableFile=new StableFile<TransactionStatus>(name);
    }
    
    public abstract List<TransactionStatus.Type> getPersistentStatuses();
    
    public void write(TransactionStatus newStatus)
    {
        //PER DEBUG
        if (!getPersistentStatuses().contains(newStatus.getType()) )
            throw new UnsupportedOperationException("Trying to write status "+
                    newStatus +"on wrong registers");
        //FINE PER DEBUG
        
        
        list.add(0,newStatus);
        this.stableFile.write(newStatus);
        //saveToDisk(newStatus);
        //stampare l'oggetto TransactionStatus su file
    }
    
    public abstract String getNameFile();
    
/*    
    public void saveToDisk(TransactionStatus status)
    {
        try{
            JAXBContext context = JAXBContext.newInstance(TransactionStatus.class); 
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            OutputStream w = null;
            w = new FileOutputStream(getNameFile());
            m.marshal(status,w);
            w.close();
       }catch(Exception ex){
           System.out.println(ex);
           System.exit(-5566);
       }    
    }
*/
    /*
    public TransactionStatus getLastStatus()
    {
        if (this.isEmpty())
            return null;
        else return list.get(0);
    }
     * 
     */
    
    public boolean isEmpty()
    {
        return list.isEmpty();
    }
/*    
    public TransactionStatus.Type getLastStatusType()
    {
            return getLastStatus().getType();
    }
*/    
    public long getSerialNumber()
    {
        int i=0;
        //Look for the last commit
        for (
            i=0;
            i<list.size() && list.get(i).getType()!=TransactionStatus.Type.COMMIT;
            i++)
        {
            //Do nothing
        }
        
        if (i<list.size())
            return list.get(i).getSerial();
        else //No commit was made
            return 0;
    }
    
    @Override
    public String toString()
    {
        String s="log=";
        for (TransactionStatus status:list)
            s+="("+status.getSerial()+":"+status.getType()+")";
        
        return s;
    }
    
}
